<template>
  <el-main>
    <div align="left">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="真实姓名">
          <el-input v-model="queryForm.trueName" size="mini" placeholder="请输入真实姓名"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="mini" plain @click="onSubmit">搜索</el-button>
          <el-button type="success" size="mini" plain @click="flush('queryForm')">清空</el-button>
          <el-button type="info" size="mini" @click="addUser()">添加用户</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      border
      :header-cell-style="{background:'#FF8800',color:'#ffffff'}"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="id" label="编号" width="100px"></el-table-column>
      <el-table-column prop="bz" label="权限"></el-table-column>
      <el-table-column prop="trueName" label="真实姓名"></el-table-column>
      <el-table-column prop="userName" label="用户名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="remarks" label="备注部分"></el-table-column>
      <el-table-column prop="rid" v-if="show"></el-table-column>
      <el-table-column prop="rname" label="拥有角色"></el-table-column>

      <el-table-column label="角色设置">
        <template slot-scope="scope">
          <el-button @click="quanxian(scope.row)" type="text" size="small">
            <i class="el-icon-s-tools"></i>
          </el-button>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="bianji(scope.row)" type="text" size="small">
            <i class="el-icon-edit-outline"></i>
          </el-button>
          <el-button type="text" size="small" @click="del(scope.row.id)">
            <i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <span>
        <el-form :inline="true" :model="saveUserForm" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input size="mini" v-model="saveUserForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="权限">
            <el-input size="mini" v-model="saveUserForm.bz"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input size="mini" type="password" v-model="saveUserForm.password"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名">
            <el-input size="mini" v-model="saveUserForm.trueName"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input size="mini" v-model="saveUserForm.remarks"></el-input>
          </el-form-item>

          <el-form-item label="角色">
            <el-checkbox-group v-model="roleCheckList">
              <el-checkbox-button
                v-for="role in roleList"
                :label="role.id"
                :key="role.id"
              >{{role.name}}</el-checkbox-button>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddOption">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 角色列表 -->
    <el-dialog title="拥有角色" :visible.sync="dialogTableVisible" @close="clearrole">
      <el-checkbox-group v-model="roleCheckList">
        <el-checkbox v-for="role in roleLists" :label="role.id+''" :key="role.id">{{role.name}}</el-checkbox>
      </el-checkbox-group>
      <br />
      <br />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRole()">确 定</el-button>
      </span>
    </el-dialog>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      show: false,
      dialogVisible: false,
      saveUserForm: {
        userName: "",
        password: ""
      },
      queryForm: {
        trueName: ""
      },
      dialogTableVisible: false,
      roleLists: [],
      roleList: [],
      roleCheckList: [],
      title: "",
      user: {}
    };
  },

  methods: {
    clearrole() {
      ths.roleCheckList = [];
    },
    saveRole() {
      console.log("asdsas===", this.roleCheckList);
      let roleList = {};
      this.user.rid = this.roleCheckList;
      this.axios({
        method: "post",
        url:
          this.requestUrl +
          "user/user/userrole?access_token=" +
          localStorage.getItem("access_token"),
        data: this.qs.stringify(this.user)
      }).then(res => {
        if ((res.data.code = "200")) {
          this.$notify({
            title: res.data.msg,
            message: "这是一条提示的提示消息",
            type: "success"
          });
          this.id = 0;
          this.dialogTableVisible = false;
          this.getUserList();
        }
        if ((res.data.code = "400")) {
          this.$notify.info({
            title: res.data.msg,
            message: "这是一条提示的提示消息"
          });
          this.dialogTableVisible = false;
          this.getUserList();
        }
      });
    },

    getUserList() {
      var $this = this;
      $this
        .axios({
          method: "get",
          url: this.requestUrl + "user/user/userList",
          params: {
            access_token: localStorage.getItem("access_token"),
            trueName: this.queryForm.trueName
          }
        })
        .then(res => {
          $this.tableData = res.data.obj;
        });
    },

    flush(queryForm) {
      this.queryForm = {};
    },

    onSubmit() {
      this.getUserList();
    },

    quanxian(row) {
      console.log("角色信息----->{}", row);
      this.dialogTableVisible = true;
      if (row.rid != null) {
        this.roleCheckList = row.rid.split(",");
      }
        this.user=row;
      console.log(this.roleLists);
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },

    addUser() {
      this.title = "添加用户";
      this.roleCheckList = [];
      this.saveUserForm = {};

      this.dialogVisible = true;
    },

    saveAddOption() {
      console.log("角色列表信息是", this.saveUserForm);
      this.saveUserForm.rid = this.roleCheckList;
      this.axios({
        method: "post",
        url:
          this.requestUrl +
          "user/user/save?access_token=" +
          localStorage.getItem("access_token"),
        data: this.qs.stringify(this.saveUserForm)
      }).then(res => {
        if ((res.data.code = "200")) {
          this.$notify({
            title: "成功",
            message: "这是一条成功的提示消息",
            type: "success"
          });
          this.dialogVisible = false;
          this.getUserList();
        } else {
          this.$notify.error({
            title: "错误",
            message: "这是一条错误的提示消息"
          });
          this.dialogVisible = false;
          this.getUserList();
        }
      });
    },

    bianji(row) {
      this.title = "修改用户";
      console.log(row);
      this.saveUserForm = {};
      this.dialogVisible = true;

      this.saveUserForm.id = row.id;
      this.saveUserForm.userName = row.userName;
      this.saveUserForm.password = row.password;
      this.roleCheckList = row.rname.split(",");
    },

    del(id) {
      this.axios({
        method: "get",
        url:
          this.requestUrl +
          "user/user/delById?access_token=" +
          localStorage.getItem("access_token"),
        params: { id: id }
      }).then(res => {
        if ((res.data.code = "200")) {
          this.$notify({
            title: "成功",
            message: res.data.msg,
            type: "success"
          });
          this.getUserList();
        } else {
          this.$notify.error({
            title: "失败",
            message: res.data.msg
          });
        }
      });
    }
  },

  mounted() {
    this.getUserList();
    this.axios({
      method: "get",
      url: this.requestUrl + "user/user/role/roleList",
      params: { access_token: localStorage.getItem("access_token") }
    }).then(res => {
      this.roleList = res.data.obj;
      this.roleLists = res.data.obj;
      console.log("this.roleList----->{}", this.roleList);
    });
  }
};
</script>

<style>
</style>